import WorkerBase
import MSData
import MSLogger

import socket
import datetime

class Worker(WorkerBase.WorkerBase):
    def __init__(self, Host, Port):
        self.DBHost = Host
        self.DBPort = int(Port)
        self.connection = None

    def getConnectionName(self):
        return '(CESAR:%s:%s)' % (str(self.DBHost), str(self.DBPort))
    def getConnection(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((self.DBHost, self.DBPort))
        return sock
    def isConnectionGood(self):
        return False 

    def sendEvent(self, Event):
        packet  = chr(1)                            #0
        packet += str(Event.objectId) + chr(2)      #1
        packet += str(Event.eventGenerateTime.strftime(MSData.TIME_FORMAT_CESAR)) + chr(2) #2
        packet += 'message' + chr(2)                #3
        packet += str(Event.contactIdZona) + chr(2) #4
        packet += 'ZoneName' + chr(2)               #5
        packet += 'SectionNumber' + chr(2)          #6
        packet += str(Event.contactIdFull) + chr(2) #7
        packet += 'surguard' + chr(2)               #8
        packet += 'line' + chr(2)                   #9
        packet += 'receiver' + chr(2)               #10
        packet += 'messageid' + chr(2)              #11
        packet += 'personId' + chr(2)               #12
        packet += 'messageGroup' + chr(2)           #13
        packet += chr(3)                            #14
        packet += 'comn' + chr(2)                   #15
        self.connection.send(packet)

    def writeEventsStatic(self, MSObjects):
        self.connect()
        for obj in MSObjects:
            try:
                doneNum         = 0
                canClearEvents  = True
                obj.eventsPGSM.sort()
                for e in obj.eventsPGSM:
                    try:
                        self.sendEvent(e)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: can\'t send event: %s' % (str(err)))
                        canClearEvents = False
                        continue
                MSLogger.logInfo('Worker::writeEventsStatic: object(%s) PGSM done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsPGSM)))
                #if canClearEvents:
                #    obj.eventsPGSM = []

                doneNum         = 0
                canClearEvents  = True
                obj.eventsContactId.sort()
                for e in obj.eventsContactId:
                    try:
                        self.sendEvent(e)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: can\'t send event: %s' % (str(err)))
                        canClearEvents = False
                        continue
                MSLogger.logInfo('Worker::writeEventsStatic: object(%s) ContactId done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsPGSM)))
                #if canClearEvents:
                #    obj.eventsContactId = []

            except Exception, err:
                MSLogger.logError('Worker::writeEventsStatic: object(%s) process failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        
